//
//  CXConfigMgr.swift
//  CXRobotServer
//
//  Created by ezreal on 2019/5/19.
//

import Foundation
import SQLite

class CXConfigMgr: NSObject {
    
    static let shared = CXConfigMgr()
    
    var m_db:Connection! = nil
    private let m_Config = Table("config")
    
    private let m_Id = Expression<Int64>("id")
    private let m_Phone = Expression<String?>("phone")
    private let m_DeviceType = Expression<String?>("device_type")
    private let m_DeviceId = Expression<String?>("device_id")
    private let m_Token = Expression<String?>("token")
    private let m_Password = Expression<String?>("password")
    
    func create(){
        do{
            m_db = try Connection("\(Bundle.main.bundlePath)/database.sqlite3")
            
            try m_db.run(m_Config.create(temporary: false, ifNotExists: true, withoutRowid: false, block: { (t) in
                t.column(m_Id, primaryKey: true)
                t.column(m_Phone, unique: true)
                t.column(m_DeviceType)
                t.column(m_DeviceId)
                t.column(m_Token)
                t.column(m_Password)
            }))
            
            log.debug("数据库创建成功:\(Bundle.main.bundlePath)/database.sqlite3")
        }catch{
            log.error("数据库创建错误:\(error)")
        }
    }
    
    func saveAccount(phone:String,deviceType:String,deviceId:String,token:String,password:String) {
        
        let alice = m_Config.filter(m_Phone == phone)
        do {
            let count = try m_db.scalar(alice.count)
            if (count == 0) {
                let insert = m_Config.insert(m_Phone <- phone, m_DeviceType <- deviceType,m_DeviceId <- deviceId,m_Token <- token,m_Password <- password)
                let rowid = try m_db.run(insert)
                log.debug("插入一条新的配置ID:\(rowid) deviceId:\(deviceId)")
            }else{
                let rowid = try m_db.run(alice.update(m_DeviceType <- deviceType,m_DeviceId <- deviceId,m_Token <- token,m_Password <- password))
                log.debug("更新一条配置ID:\(rowid) deviceId:\(deviceId)")
            }
        } catch  {
            log.error("数据库保存错误:\(error)")
        }
    }
    
    func saveConfig(deviceId:String,data:Data) {
        
//        let alice = m_Config.filter(m_DeviceId == deviceId)
//        do {
//            let count = try m_db.scalar(alice.count)
//            if (count == 0) {
//                let insert = m_Config.insert(m_DeviceId <- deviceId, m_Data <- data.datatypeValue)
//                let rowid = try m_db.run(insert)
//                log.debug("插入一条新的配置ID:\(rowid) deviceId:\(deviceId)")
//            }else{
//                let rowid = try m_db.run(alice.update(m_Data <- data.datatypeValue))
//                log.debug("更新一条配置ID:\(rowid) deviceId:\(deviceId)")
//            }
//        } catch  {
//            log.error("数据库保存错误:\(error)")
//        }
    }
    
    func getConfig(deviceId:String) -> Data {
        
//        let alice = m_Config.filter(m_DeviceId == deviceId)
//
//        do {
//
//            let user =  Array(try m_db.prepare(alice)).first
//            if (user != nil) {
//                return Data.fromDatatypeValue(try user!.get(m_Data))
//            }
//        } catch {
//
//        }

        
        return Data()
    }
}
